package IO;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-08-27
 * Time: 16:39
 */
public class Demo10 {
    public static void main(String[] args) {
        System.out.print("请输入源文件路径： ");
        Scanner scanner = new Scanner(System.in);
        String srcPath = scanner.next();
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()) {
            System.out.println("输入的源文件路径有误！");
            return;
        }
        System.out.print("输入需要复制的目标路径： ");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        if(!destFile.getParentFile().isDirectory()) {
            System.out.println("目标路径有误！");
            return;
        }

        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                outputStream.write(buffer,0,n);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
